package mypack;

import java.io.*;

public class DES {

    public static void Encrypt(String plainpath, String cipherpath, String keypath) {
        try{
            InputStream in = new BufferedInputStream(new FileInputStream(plainpath));
            OutputStream out = new BufferedOutputStream(new FileOutputStream(cipherpath));
            BufferedReader rd = new BufferedReader(new InputStreamReader(new FileInputStream(keypath)));//读取密钥
            StringBuilder initkey = new StringBuilder(rd.readLine().trim());
            StringBuilder[] key = Key.EncrykeyGenerator(initkey);
            byte[] buffer = new byte[8];
            int len = in.read(buffer);
            while(len != -1) {
                if(len != 8) {
                    byte fill = (byte)(8 - len);
                    //byte fill = (byte)0;
                    for(int i = 7; i > len - 1; i --)
                        buffer[i] = fill;
                }
                StringBuilder pp = numberChange.byteToBin(buffer);//将8个字节，即64位，以64位二进制表示
                StringBuilder secret = StaticChange.encryptProcess(pp, key);
                byte[] se = numberChange.binToByte(secret.toString());
                out.write(se);
                out.flush();
                len = in.read(buffer);
            }
            in.close();
            out.close();
        }catch(Exception e) {
            System.out.println(e.getStackTrace());
        }
    }

    public static void Decipher(String ciperpath, String plainpath, String keypath) {
        try{
            InputStream in = new BufferedInputStream(new FileInputStream(ciperpath));
            OutputStream out = new BufferedOutputStream(new FileOutputStream(plainpath));
            BufferedReader rd = new BufferedReader(new InputStreamReader(new FileInputStream(keypath)));//读取密钥
            StringBuilder initkey = new StringBuilder(rd.readLine().trim());
            StringBuilder[] key = Key.DeciKeyGenerator(initkey);
            byte[] buffer = new byte[8];
            int len = in.read(buffer);
            while(len != -1) {
                if(len != 8) {
                    byte fill = (byte)(8 - len);
                    for(int i = 7; i > len - 1; i --)
                        buffer[i] = fill;
                }
                StringBuilder pp = numberChange.byteToBin(buffer);//将8个字节，即64位，以64位二进制表示
                StringBuilder secret = StaticChange.deciProcess(pp, key);
                byte[] se = numberChange.binToByte(secret.toString());
                out.write(se);
                out.flush();
                len = in.read(buffer);
            }
            in.close();
            out.close();
        }catch(Exception e) {
            System.out.println(e.getStackTrace());
        }
    }

    

    public static void main(String[] args) {
        //StringBuilder[] key = Key.EncrykeyGenerator(new StringBuilder(args[0]));
        //System.out.println(encryptProcess(new StringBuilder(args[1]), key));
        //StringBuilder[] key = Key.DeciKeyGenerator(new StringBuilder(args[0]));
        //System.out.println(deciProcess(new StringBuilder(args[1]), key));
        //Decipher(args[0], args[1], args[2]);
        Encrypt(args[0], args[1], args[2]);
    }
}